package mbg;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class MySQLCommentGenerator extends EmptyCommentGenerator {

    private TopLevelClass topLevelClass;
    private final Properties properties;

    public MySQLCommentGenerator() {
        properties = new Properties();
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        // 获取自定义的 properties
        this.properties.putAll(properties);
        Enumeration<?> enu = this.properties.propertyNames();
        while (enu.hasMoreElements()) {
            Object key = enu.nextElement();
        }
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        this.topLevelClass = topLevelClass;
        String author = properties.getProperty("author");
        String dateFormat = properties.getProperty("dateFormat", "yyyy-MM-dd");
        SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
        String remarks = introspectedTable.getRemarks();
        // 添加实体类导入的包
        topLevelClass.addImportedType("io.swagger.annotations.ApiModel");
        topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");
        topLevelClass.addImportedType("lombok.Data;");
        topLevelClass.addImportedType("javax.persistence.Table");
        topLevelClass.addImportedType("javax.persistence.Id");
        // 添加实体类注解
        topLevelClass.addAnnotation("@ApiModel(\"" + remarks + "\")");
        topLevelClass.addAnnotation("@Table(name = \"" + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + "\")");
        topLevelClass.addAnnotation("@Data");
        // 获取表注释

        topLevelClass.addImportedType("");
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * " + remarks);
        topLevelClass.addJavaDocLine(" *");
        topLevelClass.addJavaDocLine(" * @author " + author);
        topLevelClass.addJavaDocLine(" * @date   " + dateFormatter.format(new Date()));
        topLevelClass.addJavaDocLine(" */");
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        List<String> collect = introspectedTable.getPrimaryKeyColumns().stream().map(IntrospectedColumn::getActualColumnName).collect(Collectors.toList());
        // 获取列注释
        String remarks = introspectedColumn.getRemarks();
        // 添加jsr303验证
        if(!introspectedColumn.isNullable() && !collect.contains(introspectedColumn.getActualColumnName())){
            String msg = "\"" + remarks + "不能为空\"";
            field.addAnnotation("@NotBlank(message = " + msg + ")");
            topLevelClass.addImportedType("javax.validation.constraints.NotBlank");
        }
        // 添加 java persistence id
        if(collect.contains(introspectedColumn.getActualColumnName())){
            field.addAnnotation("@Id");
            if(introspectedColumn.getJdbcTypeName().equals("VARCHAR")){
                topLevelClass.addImportedType("tk.mybatis.mapper.annotation.KeySql");
                field.addAnnotation("@KeySql(genId = UuIdGenId.class)");
            }
        }

        // 添加属性注解
        field.addAnnotation("@ApiModelProperty(\"" + remarks +  "\")");

        // 添加属性注释
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * " + remarks);
        field.addJavaDocLine(" */");
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
        super.addClassComment(innerClass, introspectedTable);
    }
}
